﻿using System;
using System.Linq;
using System.Collections.Generic;
using VladanStrigoNET.Samples.Blogged.Models;
using PostModel = VladanStrigoNET.Samples.Blogged.Models.Post;

namespace VladanStrigoNET.Samples.Blogged.DataAccess
{
    public class LinqPostRepository : IPostRepository
    {

        public IList<PostModel> GetTopPosts()
        {
            using (BlogDataContext context = _CreateDataContext())
            {
                var query = from p in context.Posts
                            orderby p.PostedOn descending
                            select p;
                return query.ToList().ConvertAll<Models.Post>(p => _ModelPostFromDataAccessPost(p));
            }
        }

        public IList<PostModel> GetPostsSince(DateTime startDate)
        {
            using (BlogDataContext db = _CreateDataContext())
            {
                var query = from p in db.Posts
                            where p.PostedOn >= startDate
                            orderby p.PostedOn descending
                            select p;
                return query.ToList().ConvertAll(p => _ModelPostFromDataAccessPost(p));
            }
        }

        public IList<PostModel> GetPostsDuring(DateTime startDate, DateTime endDate)
        {
            using (BlogDataContext db = _CreateDataContext())
            {
                var query = from p in db.Posts
                            where p.PostedOn >= startDate && p.PostedOn <= endDate
                            orderby p.PostedOn descending
                            select p;
                return query.ToList().ConvertAll(p => _ModelPostFromDataAccessPost(p));
            }
        }

        public PostModel GetPostById(int id)
        {
            using (BlogDataContext db = _CreateDataContext())
            {
                var query = from p in db.Posts
                            where p.Id == id
                            select p;
                var posts = query.ToList();
                if (posts.Count == 0)
                {
                    return null;
                }
                return _ModelPostFromDataAccessPost(posts[0]);
            }
        }

        public void Delete(int id)
        {
            using (BlogDataContext db = _CreateDataContext())
            {
                Post post = db.Posts.Single(p => p.Id == id);
                db.Posts.DeleteOnSubmit(post);
                db.SubmitChanges();
            }
        }
        public void AddNewPost(string title, string body)
        {
            Post newPost = new Post() { Title = title, Body = body, PostedOn = DateTime.Now };
            using (BlogDataContext db = _CreateDataContext())
            {
                db.Posts.InsertOnSubmit(newPost);
                db.SubmitChanges();
            }
        }

        private static BlogDataContext _CreateDataContext()
        {
            BlogDataContext context = new BlogDataContext();
            context.ObjectTrackingEnabled = true;
            return context;
        }
        private static PostModel _ModelPostFromDataAccessPost(DataAccess.Post p)
        {
            return new Models.Post { Id = p.Id, Title = p.Title, PostedOn = p.PostedOn, Body = p.Body };
        }
    }
}
